package com.example.itshome.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import com.example.itshome.R;
import com.example.itshome.adapter.MainPageFragmentAdapter;
import com.example.itshome.entity.CommodityEntity;
import com.example.itshome.fragment.ShopItemFragment;
import com.example.itshome.network.AbstractRecycleVIewGetRequest;
import com.example.itshome.network.GetSearchCommodityRequest;

import java.util.ArrayList;
import java.util.List;

public class ShopPageActivity extends AppCompatActivity implements View.OnClickListener {


    private ImageView backImageView;
    private ViewPager shopViewPager;
    private TextView[] shopChoseTextViews;
    private EditText searchEditText;
    private List<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_page);
        initFind();
        initTextView();
        initButton();
        initViewPager();
        initView();
    }

    private void initFind(){
        backImageView=findViewById(R.id.imv_shop_page_back);
        shopViewPager=findViewById(R.id.vp_shop_page);
        shopChoseTextViews=new TextView[4];
        shopChoseTextViews[0]=findViewById(R.id.txv_shop_page_item1);
        shopChoseTextViews[1]=findViewById(R.id.txv_shop_page_item2);
        shopChoseTextViews[2]=findViewById(R.id.txv_shop_page_item3);
        shopChoseTextViews[3]=findViewById(R.id.txv_shop_page_item4);
        searchEditText=findViewById(R.id.edt_shop_page_search);
    }

    void initView(){
        //搜索框
        searchEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if(i== EditorInfo.IME_ACTION_SEARCH){
                    ShopItemFragment fragment= ((ShopItemFragment)fragments.get(0));
                    AbstractRecycleVIewGetRequest<CommodityEntity> request=new GetSearchCommodityRequest(ShopPageActivity.this)
                            .setSearchValue(searchEditText.getText().toString())
                            .setAdapter(fragment.mRecycleView.getAdapter())
                            .setCurrentData(fragment.mRecycleViewData);
                    request.start();

                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);

                }
                return false;
            }
        });
    }

    private void initTextView(){
        for (int i=0;i<4;i++){
            shopChoseTextViews[i].setTag(i+1);
            shopChoseTextViews[i].setOnClickListener(this);
        }
        pageChangeTo(0);
    }


    private void initButton(){
        backImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initViewPager(){
        fragments=new ArrayList<>();
        fragments.add(new ShopItemFragment());
        fragments.add(new ShopItemFragment());
        fragments.add(new ShopItemFragment());
        fragments.add(new ShopItemFragment());
        Bundle b1=new Bundle();
        b1.putString("pageType", ShopItemFragment.CommodityPageType.All.name());
        fragments.get(0).setArguments(b1);
        Bundle b2=new Bundle();
        b2.putString("pageType", ShopItemFragment.CommodityPageType.Dog.name());
        fragments.get(1).setArguments(b2);
        Bundle b3=new Bundle();
        b3.putString("pageType", ShopItemFragment.CommodityPageType.Cat.name());
        fragments.get(2).setArguments(b3);
        Bundle b4=new Bundle();
        b4.putString("pageType", ShopItemFragment.CommodityPageType.Other.name());
        fragments.get(3).setArguments(b4);

        shopViewPager.setAdapter(new MainPageFragmentAdapter(getSupportFragmentManager(),fragments));
        shopViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                pageChangeTo(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void pageChangeTo(int position){
        for(TextView textView:shopChoseTextViews){
            textView.setSelected(textView.getTag().equals(position+1));
        }
    }


    @Override
    public void onClick(View view) {
        int vid=(int)view.getTag();
        pageChangeTo(vid-1);
        shopViewPager.setCurrentItem(vid-1);
    }

}